package cn.stylefeng.guns.modular.myorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.myorder.entity.Myorder;
import cn.stylefeng.guns.modular.myorder.enums.MyorderExceptionEnum;
import cn.stylefeng.guns.modular.myorder.mapper.MyorderMapper;
import cn.stylefeng.guns.modular.myorder.pojo.request.MyorderRequest;
import cn.stylefeng.guns.modular.myorder.service.MyorderService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单管理业务实现层
 *
 * @author zhu
 * @date 2021/09/25 18:20
 */
@Service
public class MyorderServiceImpl extends ServiceImpl<MyorderMapper, Myorder> implements MyorderService {

    @Override
    public void add(MyorderRequest myorderRequest) {
        Myorder myorder = new Myorder();
        BeanUtil.copyProperties(myorderRequest, myorder);
        this.save(myorder);
    }

    @Override
    public void del(MyorderRequest myorderRequest) {
        Myorder myorder = this.queryMyorder(myorderRequest);
        this.removeById(myorder.getOrderId());
    }

    @Override
    public void edit(MyorderRequest myorderRequest) {
        Myorder myorder = this.queryMyorder(myorderRequest);
        BeanUtil.copyProperties(myorderRequest, myorder);
        this.updateById(myorder);
    }

    @Override
    public Myorder detail(MyorderRequest myorderRequest) {
        return this.queryMyorder(myorderRequest);
    }

    @Override
    public PageResult<Myorder> findPage(MyorderRequest myorderRequest) {
        LambdaQueryWrapper<Myorder> wrapper = createWrapper(myorderRequest);
        Page<Myorder> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Myorder> findList(MyorderRequest myorderRequest) {
        LambdaQueryWrapper<Myorder> wrapper = this.createWrapper(myorderRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhu
     * @date 2021/09/25 18:20
     */
    private Myorder queryMyorder(MyorderRequest myorderRequest) {
        Myorder myorder = this.getById(myorderRequest.getOrderId());
        if (ObjectUtil.isEmpty(myorder)) {
            throw new BusinessException(MyorderExceptionEnum.MYORDER_NOT_EXISTED);
        }
        return myorder;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhu
     * @date 2021/09/25 18:20
     */
    private LambdaQueryWrapper<Myorder> createWrapper(MyorderRequest myorderRequest) {
        LambdaQueryWrapper<Myorder> queryWrapper = new LambdaQueryWrapper<>();

        Long orderId = myorderRequest.getOrderId();
        String orderName = myorderRequest.getOrderName();
        String orderUser = myorderRequest.getOrderUser();
        java.math.BigDecimal orderPrice = myorderRequest.getOrderPrice();
        String orderShop = myorderRequest.getOrderShop();
        String orderUserAddress = myorderRequest.getOrderUserAddress();
        String orderUserPhone = myorderRequest.getOrderUserPhone();
        String createTime = myorderRequest.getCreateTime();
        Long createUser = myorderRequest.getCreateUser();
        String updateTime = myorderRequest.getUpdateTime();
        Long updateUser = myorderRequest.getUpdateUser();

        queryWrapper.eq(ObjectUtil.isNotNull(orderId), Myorder::getOrderId, orderId);
        queryWrapper.like(ObjectUtil.isNotEmpty(orderName), Myorder::getOrderName, orderName);
        queryWrapper.like(ObjectUtil.isNotEmpty(orderUser), Myorder::getOrderUser, orderUser);
        queryWrapper.eq(ObjectUtil.isNotNull(orderPrice), Myorder::getOrderPrice, orderPrice);
        queryWrapper.like(ObjectUtil.isNotEmpty(orderShop), Myorder::getOrderShop, orderShop);
        queryWrapper.like(ObjectUtil.isNotEmpty(orderUserAddress), Myorder::getOrderUserAddress, orderUserAddress);
        queryWrapper.like(ObjectUtil.isNotEmpty(orderUserPhone), Myorder::getOrderUserPhone, orderUserPhone);
        queryWrapper.eq(ObjectUtil.isNotNull(createTime), Myorder::getCreateTime, createTime);
        queryWrapper.eq(ObjectUtil.isNotNull(createUser), Myorder::getCreateUser, createUser);
        queryWrapper.eq(ObjectUtil.isNotNull(updateTime), Myorder::getUpdateTime, updateTime);
        queryWrapper.eq(ObjectUtil.isNotNull(updateUser), Myorder::getUpdateUser, updateUser);

        return queryWrapper;
    }

}